<?php

function reequipment_userapi_getmaintenance($args)
{
    extract($args);

    /* Fields in table.
		maintenance_id
		equipment_id
		company_id
		schedule_id
		frequency_id
		date_scheduled
		date_complete
		issue
		resolution
		item_ids
		manhours
		downtime
   */

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}
	// Change this to a variable in the strtotime.  Need to look it up where used now.
	if (isset($date_due)){
		$date_due = strtotime($date_due); 
	}
	if (isset($over_due)){
		$over_due = time();
		$scheduled = true;
	}
	
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['remaintenance'];
    $join_equipment = $xartable['reequipment'];
    $join_model = $xartable['remodels'];
    $join_area = $xartable['reareas'];
    $join_company = $xartable['rel_eq'];
    $join_scheduled = $xartable['rescheduled'];
    $join_frequency = $xartable['refrequency'];
    $join_rel_frequency = $xartable['refrequency'];
    
    $query = "SELECT DISTINCT ma.maintenance_id,
					ma.equipment_id,
					ma.company_id,
					ma.schedule_id,
					ma.date_scheduled,
					ma.date_due,
					ma.cycles_due,
					ma.date_complete,
					ma.issue,
					ma.resolution,
					ma.item_ids,
					ma.manhours,
					ma.downtime,
					e.serial_number,
					e.cycles,
					e.machine_name,
					m.model_name,
					a.area_name,
					r.company_name,
					s.manhours as projected_manhours,
					f.frequency_id,
					f.frequency,
					ma.frequency_id as rel_frequency_id,
					rf.frequency as rel_frequency_name"
    		. " FROM $table ma"
    		
    		. " LEFT OUTER JOIN $join_equipment e"
    		. " ON ma.equipment_id = e.equipment_id AND e.inactive IS NULL"        		
    		
    		. " LEFT OUTER JOIN $join_model m"
    		. " ON e.model_id = m.model_id"
    		
    		. " LEFT OUTER JOIN $join_area a"
    		. " ON m.area_id = a.area_id"
    		
    		. " LEFT OUTER JOIN $join_company r"
    		. " ON ma.company_id = r.company_id"
    		
    		. " LEFT OUTER JOIN $join_scheduled s"
    		. " ON ma.schedule_id = s.schedule_id"
    		
    		. " LEFT OUTER JOIN $join_frequency f"
    		. " ON s.frequency_id = f.frequency_id"
    		
    		. " LEFT OUTER JOIN $join_rel_frequency rf"
    		. " ON ma.frequency_id = rf.frequency_id";    

    $where = array();
    $bind = array();
    
    if (!empty($maintenance_id)) {
        $where[] = 'ma.maintenance_id = ?';
        $bind[] = (int)$maintenance_id;
    }
    
    if (!empty($equipment_id)) {
        $where[] = 'ma.equipment_id = ?';
        $bind[] = (int)$equipment_id;
    }

    if (!empty($schedule_id)) {
        $where[] = 'ma.schedule_id = ?';
        $bind[] = (int)$schedule_id;
    }  
    
    if (!empty($company_id)) {
        $where[] = 'ma.company_id = ?';
        $bind[] = (int)$company_id;
    }      
    if (!empty($area_id)) {
        $where[] = 'a.area_id = ?';
        $bind[] = (int)$area_id;
    } 
    if (!empty($model_id)) {
        $where[] = 'm.model_id = ?';
        $bind[] = (int)$model_id;
    } 
    
    if (!empty($frequency_id)) {
        $where[] = 'ma.frequency_id = ?';
        $bind[] = (int)$frequency_id;
    } 
    if (!empty($date_due)) {
        $where[] = 'ma.date_due < ?';
        $bind[] = (int)$date_due;
    }      
    if (!empty($over_due)) {
        $where[] = 'ma.date_due < ?';
        $bind[] = (int)$over_due;
    }   
    if (!empty($scheduled)) {
        $where[] = 'ma.schedule_id != ?';
        $bind[] = (int)0;
    }      	
    // Figure out the dates when we need it.
    
    if (!empty($date_complete)) {
        $where[] = 'ma.date_complete = 0';
    }      
    if (!empty($complete)) {
        $where[] = 'ma.date_complete > 0';
    }    

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY ma.date_scheduled";
    // return var_export($query);

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($maintenance_id,
			$equipment_id,
			$company_id,
			$schedule_id,
			$date_scheduled,
			$date_due,
			$cycles_due,
			$date_complete,
			$issue,
			$resolution,
			$item_ids,
			$manhours,
			$downtime,
			$serial_number,
			$cycles,
			$machine_name,
			$model_name,
			$area_name,
			$company_name,
			$projected_manhours,
			$frequency_id,
			$frequency_name,
			$rel_frequency_id,
			$rel_frequency_name) = $result->fields;

        $tags[] = array(
        		'maintenance_id'		=> $maintenance_id,
        		'equipment_id'			=> $equipment_id,
        		'company_id'				=> $company_id,
        		'schedule_id'				=> $schedule_id,
        		'date_scheduled'			=> xarLocaleGetFormattedDate('long', $date_scheduled),
        		'date_due'					=> xarLocaleGetFormattedDate('long', $date_due),
        		'cycles_due'				=> $cycles_due,
        		'date_complete'			=> xarLocaleGetFormattedDate('long', $date_complete),
        		'issue'						=> xarVarPrepForDisplay($issue),
        		'resolution'					=> xarVarPrepForDisplay($resolution),
                'item_ids'      				=> $item_ids,
                'manhours'      			=> $manhours,
                'downtime'					=> $downtime,
                'serial_number'			=> xarVarPrepForDisplay($serial_number),
                'cycles' 						=> $cycles,
                'machine_name'    		=> xarVarPrepForDisplay($machine_name),
                'model_name'    		=> xarVarPrepForDisplay($model_name),
                'area_name'    			=> xarVarPrepForDisplay($area_name), 
                'company_name'    	=> xarVarPrepForDisplay($company_name), 
                'projected_manhours'	=> $projected_manhours,
                'frequency_id'			=> $frequency_id,
                'frequency_name'    	=> xarVarPrepForDisplay($frequency_name), 
                'rel_frequency_id' 		=> $rel_frequency_id,
                'rel_frequency_name' => xarVarPrepForDisplay($rel_frequency_name)
        );
    }
    $result->Close();
    return $tags;
}
?>